package com.amap.map2d.demo;

import android.app.Dialog;
import android.app.ListActivity;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;

import com.amap.api.maps2d.model.LatLng;
import com.amap.map2d.demo.overlay.HotMarkerActivity;
import com.amap.map2d.demo.overlay.MarkerActivity;
import com.amap.map2d.demo.overlay.MarkerClickActivity;
import com.amap.map2d.demo.util.CallbackBundle;
import com.amap.map2d.demo.util.OpenFileDialog;
import com.amap.map2d.demo.util.SQLiteDbHelper;
import com.amap.map2d.demo.view.FeatureView;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

/**
 * 主菜单
 */
public final class AdminActivity extends ListActivity {


    SQLiteDbHelper helper;
    SQLiteDatabase database;

    private static class DemoDetails {
        private final int titleId;
        private final int descriptionId;
        private final String name;

        public DemoDetails(int titleId, int descriptionId,
                           String name) {
            super();
            this.titleId = titleId;
            this.descriptionId = descriptionId;
            this.name = name;
        }
    }


    private static class CustomArrayAdapter extends ArrayAdapter<DemoDetails> {
        public CustomArrayAdapter(Context context, DemoDetails[] demos) {
            super(context, R.layout.feature, R.id.title, demos);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            FeatureView featureView;
            if (convertView instanceof FeatureView) {
                featureView = (FeatureView) convertView;
            } else {
                featureView = new FeatureView(getContext());
            }
            DemoDetails demo = getItem(position);
            featureView.setTitleId(demo.titleId, demo.name != null);
            return featureView;
        }
    }

    private static final DemoDetails[] demos = {

            //核心功能不变---------------------------------
            //在地图上绘制
            new DemoDetails(R.string.admin_module, R.string.blank, null),
            //绘制点
            new DemoDetails(R.string.drptzb, R.string.marker_description,
                    "1"),
            //绘制点
            new DemoDetails(R.string.drrlzb, R.string.marker_description,
                    "2"),
            //绘制点
            new DemoDetails(R.string.drdlzb, R.string.marker_description,
                    "3"),

            new DemoDetails(R.string.scptzb, R.string.marker_description,
                    "4"),
            //绘制点
            new DemoDetails(R.string.scrlzb, R.string.marker_description,
                    "5"),
            //绘制点
            new DemoDetails(R.string.scdlzb, R.string.marker_description,
                    "6"),
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_activity);
        setTitle("GPS数据");
        ListAdapter adapter = new CustomArrayAdapter(
                this.getApplicationContext(), demos);
        setListAdapter(adapter);

        helper = new SQLiteDbHelper(getApplicationContext());
        database = helper.getWritableDatabase();
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        System.exit(0);
    }

    @Override
    protected Dialog onCreateDialog(final int id) {
        if (id == 1 || id == 2 || id == 3) {
            Map<String, Integer> images = new HashMap<String, Integer>();
            // 下面几句设置各文件类型的图标， 需要你先把图标添加到资源文件夹
            images.put(OpenFileDialog.sRoot, R.drawable.filedialog_root);    // 根目录图标
            images.put(OpenFileDialog.sParent, R.drawable.filedialog_folder_up);    //返回上一层的图标
            images.put(OpenFileDialog.sFolder, R.drawable.filedialog_folder);    //文件夹图标
            images.put("txt", R.drawable.filedialog_file);
            images.put(OpenFileDialog.sEmpty, R.drawable.filedialog_root);
            Dialog dialog = OpenFileDialog.createDialog(id, this, "打开文件", new CallbackBundle() {
                        @Override
                        public void callback(Bundle bundle) {
                            final String filepath = bundle.getString("path");
                            new Thread() {
                                @Override
                                public void run() {
                                    Message message1 = new Message();
                                    message1.what = 1;
                                    handler.sendMessage(message1);
                                    try {
                                        FileInputStream is = new FileInputStream(new File(filepath));
                                        InputStreamReader input = new InputStreamReader(is);
                                        BufferedReader buff = new BufferedReader(input);
                                        String temp;
                                        while ((temp = buff.readLine()) != null) {
                                            switch (id) {
                                                case 1:
                                                    database.execSQL("INSERT INTO a (latlng) VALUES ( '" + temp + "')");
                                                    break;
                                                case 2:
                                                    database.execSQL("INSERT INTO b (latlng) VALUES ( '" + temp + "')");
                                                    break;
                                                case 3:
                                                    database.execSQL("INSERT INTO c (latlng) VALUES ( '" + temp + "')");
                                                    break;
                                            }
                                        }
                                    } catch (IOException e) {
                                        e.printStackTrace();
                                    }
                                    Message message2 = new Message();
                                    message2.what = 2;
                                    handler.sendMessage(message2);
                                }
                            }.start();


                        }
                    },
                    ".txt;",
                    images);
            return dialog;
        }
        return null;
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {

        DemoDetails demo = (DemoDetails) getListAdapter().getItem(position);
        if (demo.name != null) {
            switch (demo.name) {
                case "1":
                    inserta();
                    break;
                case "2":
                    insertb();
                    break;
                case "3":
                    insertc();
                    break;
                case "4":
                    dela();
                    break;
                case "5":
                    delb();
                    break;
                case "6":
                    delc();
                    break;
            }
        }
    }


    public void inserta() {
        showDialog(1);
    }


    public void insertb() {
        showDialog(2);
    }


    public void insertc() {
        showDialog(3);
    }


    public void dela() {
        new Thread() {
            @Override
            public void run() {
                Message message1 = new Message();
                message1.what = 1;
                handler.sendMessage(message1);
                database.execSQL("DELETE FROM a");
                Message message2 = new Message();
                message2.what = 2;
                handler.sendMessage(message2);
            }
        }.start();
    }

    public void delb() {
        new Thread() {
            @Override
            public void run() {
                Message message1 = new Message();
                message1.what = 1;
                handler.sendMessage(message1);
                database.execSQL("DELETE FROM b");
                Message message2 = new Message();
                message2.what = 2;
                handler.sendMessage(message2);
            }
        }.start();
    }

    public void delc() {


        new Thread() {
            @Override
            public void run() {
                Message message1 = new Message();
                message1.what = 1;
                handler.sendMessage(message1);
                database.execSQL("DELETE FROM c");
                Message message2 = new Message();
                message2.what = 2;
                handler.sendMessage(message2);
            }
        }.start();
    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    Toast.makeText(getApplicationContext(), "处理中", Toast.LENGTH_LONG).show();
                    break;
                case 2:
                    Toast.makeText(getApplicationContext(), "处理完成", Toast.LENGTH_LONG).show();
                    break;
            }
        }
    };

}
